package com.megvii.koala.tv.data;

import android.content.Context;

import com.megvii.corelib.data.BeanManager;
import com.megvii.corelib.util.DateTimeUtil;
import com.megvii.corelib.util.SharedPreferenceTool;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

/**
 * Created by linyue on 16/3/16.
 */
public class SurveillanceCameraManager extends BeanManager<CameraBean>
{
    public static final String KEY_PREVIEW_CAMERA_ID = "KEY_PREVIEW_CAMERA_ID";

    public static final String KEY_RECOGNIZE_CAMERA_IDS = "KEY_RECOGNIZE_CAMERA_IDS";

    private Context context;

    private CameraBean previewCamera;

    private HashSet<CameraBean> recognizeCameras = new HashSet<>();

    public SurveillanceCameraManager(Context context)
    {
        super();
        this.context = context;
    }

    public SurveillanceCameraManager(Comparator<CameraBean> comparator, Context context)
    {
        super(comparator);
        this.context = context;
    }

    @Override
    protected void update(CameraBean bean, boolean sortAndIndex)
    {
        bean.box_heartbeat = DateTimeUtil.toMSTimestamp(bean.box_heartbeat);
        super.update(bean, sortAndIndex);
    }

    @Override
    public void clear()
    {
        super.clear();
        clearRecognizeCameras();
        previewCamera = null;
    }

    public void addRecognizeCamera(CameraBean camera)
    {
        recognizeCameras.add(camera);
    }

    public void addRecognizeCameras(List<CameraBean> cameras)
    {
        recognizeCameras.addAll(cameras);
    }

    public void clearRecognizeCameras()
    {
        recognizeCameras.clear();
    }

    public Collection<CameraBean> getRecognizeCameras()
    {
        return recognizeCameras;
    }

    public CameraBean getPreviewCamera()
    {
        return previewCamera;
    }

    public void setPreviewCamera(CameraBean camera)
    {
        this.previewCamera = camera;
    }

    public void persistentSave()
    {
        //
        if (previewCamera != null)
        {
            SharedPreferenceTool.saveStringValue(context, KEY_PREVIEW_CAMERA_ID, previewCamera.id);
        }
        else
        {
            SharedPreferenceTool.removeStringValue(context, KEY_PREVIEW_CAMERA_ID);
        }
        //
        List<String> recognizeCameraIds = new ArrayList<>();
        for (CameraBean camera : recognizeCameras)
        {
            recognizeCameraIds.add(camera.id);
        }
        SharedPreferenceTool
                .saveStringCollection(context, KEY_RECOGNIZE_CAMERA_IDS, recognizeCameraIds);
    }

    public void persistentClear()
    {
        SharedPreferenceTool.removeStringValue(context, KEY_PREVIEW_CAMERA_ID);
        SharedPreferenceTool.removeStringCollection(context, KEY_PREVIEW_CAMERA_ID);
    }

    public void persistentLoad()
    {
        previewCamera = null;
        clearRecognizeCameras();
        List<CameraBean> allCameras = getAll();
        //
        String previewCameraId = SharedPreferenceTool
                .getStringValueByKey(context, KEY_PREVIEW_CAMERA_ID, null);
        if (previewCameraId != null)
        {
            for (CameraBean camera : allCameras)
            {
                if (previewCameraId.equals(camera.id))
                {
                    previewCamera = camera;
                }
            }
        }
        //
        Collection<String> recognizeCameraIds = SharedPreferenceTool
                .getStringCollection(context, KEY_RECOGNIZE_CAMERA_IDS);
        if (recognizeCameraIds != null && !recognizeCameraIds.isEmpty())
        {
            for (CameraBean camera : allCameras)
            {
                if (recognizeCameraIds.contains(camera.id))
                {
                    recognizeCameras.add(camera);
                }
            }
        }
    }

    public String getPreviewCameraId(){
        String previewCameraId = SharedPreferenceTool
                .getStringValueByKey(context, KEY_PREVIEW_CAMERA_ID, null);
        return previewCameraId;
    }

    public Collection<String> getRecognizeCameraIds(){
        Collection<String> recognizeCameraIds = SharedPreferenceTool
                .getStringCollection(context, KEY_RECOGNIZE_CAMERA_IDS);
        return recognizeCameraIds;
    }

    public boolean isPersistented()
    {
        String previewCameraId = SharedPreferenceTool
                .getStringValueByKey(context, KEY_PREVIEW_CAMERA_ID, null);
        Collection<String> recognizeCameraIds = SharedPreferenceTool
                .getStringCollection(context, KEY_RECOGNIZE_CAMERA_IDS);
        return previewCameraId != null && recognizeCameraIds != null;
    }
}
